<?php

namespace App\Admin\Controllers;

use App\Models\Attendance;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class AttendanceController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '考勤数据';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Attendance());

        $grid->column('id', __('Id'));
        $grid->column('uid', __('Uid'));
        $grid->column('mac', __('Mac'));
        $grid->column('address', __('Address'));
        $grid->column('img', __('Img'))->display(function ($img){
            $text = '';
            if($img){
//                $img = url('/static/'.$img);
                $text = '<img style="width:100px;height:100px;" src="'.$img.'" />';
            }

            return $text;
        });
        $grid->column('date', __('Date'));
        $grid->column('mode', __('Mode'))->display(function ($mode){

            $text = '';
            if($mode == 1){
                $text = '办公区域打卡';
            }else if($mode == 2){
                $text = '远程打卡';
            }else if($mode == 3){
                $text = '';
            }

            return $text;
        });
        $grid->column('card_type', __('Card type'))->display(function ($mode){

            $text = '';
            if($mode == 1){
                $text = '上班';
            }else if($mode == 2){
                $text = '下班';
            }

            return $text;
        });
        $grid->column('latitude', __('Latitude'));
        $grid->column('longitude', __('Longitude'));
        $grid->column('created_at', __('Created at'));
        $grid->column('updated_at', __('Updated at'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Attendance::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('uid', __('Uid'));
        $show->field('mac', __('Mac'));
        $show->field('address', __('Address'));
        $show->field('img', __('Img'));
        $show->field('date', __('Date'));
        $show->field('mode', __('Mode'));
        $show->field('card_type', __('Card type'));
        $show->field('latitude', __('Latitude'));
        $show->field('longitude', __('Longitude'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Attendance());

        $form->number('uid', __('Uid'));
        $form->text('mac', __('Mac'));
        $form->text('address', __('Address'));
        $form->image('img', __('Img'));
        $form->datetime('date', __('Date'))->default(date('Y-m-d H:i:s'));
        $form->number('mode', __('Mode'))->default(1);
        $form->number('card_type', __('Card type'));
        $form->text('latitude', __('Latitude'));
        $form->text('longitude', __('Longitude'));

        return $form;
    }
}
